//
//  ZH_TeacherInfomationViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/11/9.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_TeacherInfomationViewController.h"
#import "ZH_TopImageTableViewCell.h"
#import "ZH_CenterInfoTableViewCell.h"
#import "ZH_BottomInfoTableViewCell.h"
#import "ZH_LookTeacherViewModel.h"
#import "ZH_HomeSourceInfoViewController.h"
#import "ZH_HomeScheduleViewController.h"
@interface ZH_TeacherInfomationViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_LookTeacherViewModel *viewModel;
@end

@implementation ZH_TeacherInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    [self initData];
    //机构课程
    [self classData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
#pragma mark - initData
- (void)initData{
    [self.viewModel.teacherDetailParam setObject:self.idString forKey:@"id"];
    [self.viewModel.teacherDetailParam setObject:self.typeString forKey:@"type"];
    WEAKSelf;
    [self.viewModel teacherDetailResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            weakSelf.dic = data[@"data"];
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
    
}
- (void)classData{
    [self.viewModel.teacherCourseParam setObject:self.idString forKey:@"id"];
    WEAKSelf;
    [self.viewModel teacherCourseResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
///更新导航栏显示
- (void)loadNavigationBarBackground{
    [((Hen_BaseNavigationViewController*)self.navigationController) setNavigationBarBgColor:[UIColor clearColor]];
    [((Hen_BaseNavigationViewController*)self.navigationController) setBackgroundHidden:YES];
    [((Hen_BaseNavigationViewController*)self.navigationController) setShadowViewHidden:YES];
}
- (void)initUserInterface{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_spshare1"] style:UIBarButtonItemStylePlain target:self action:nil];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(-[DATAMODEL KNavigationBarHeioght]);
        make.left.right.bottom.equalTo(self.view);
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        return self.viewModel.teacherCourseArray.count;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return FITSCALE(44);
    }
    else if (section == 2){
        return FITSCALE(44);
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return FITSCALE(9);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
        UIView *lineViewss = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [view addSubview:lineViewss];
        [lineViewss mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(view);
            make.height.mas_equalTo(FITSCALE(1));
        }];
        UIView *lineView = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#00C9AE"]];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.width.mas_equalTo(FITSCALE(4));
            make.height.mas_equalTo(FITSCALE(18));
            make.left.equalTo(view).offset(FITSCALE(11));
        }];
        UILabel *label = [UILabel createLabelWithText:@"详情" font:kFontSize_30];
        label.font = [UIFont boldSystemFontOfSize:15];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(lineView.mas_right).offset(FITSCALE(8));
        }];
        UIView *lineViews = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [view addSubview:lineViews];
        [lineViews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(view);
            make.height.mas_equalTo(FITSCALE(1));
        }];
        return view;
    }
    else if (section == 2){
        UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
        UIView *lineViewss = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [view addSubview:lineViewss];
        [lineViewss mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(view);
            make.height.mas_equalTo(FITSCALE(1));
        }];
        UIView *lineView = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#00C9AE"]];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.width.mas_equalTo(FITSCALE(4));
            make.height.mas_equalTo(FITSCALE(18));
            make.left.equalTo(view).offset(FITSCALE(11));
        }];
        UILabel *label = [UILabel createLabelWithText:@"预约课程" font:kFontSize_30];
        label.font = [UIFont boldSystemFontOfSize:15];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(lineView.mas_right).offset(FITSCALE(8));
        }];
        UIView *lineViews = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [view addSubview:lineViews];
        [lineViews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(view);
            make.height.mas_equalTo(FITSCALE(1));
        }];
        return view;
    }
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZH_TopImageTableViewCell *cell = [ZH_TopImageTableViewCell cellWithTableView:tableView];
        [cell updaUI:self.dic];
//        WEAKSelf;
        cell.onButtonClick = ^(NSInteger tag) {
            if (tag == 1) {
//                [weakSelf attenTion];
            }
            else if (tag == 2){
                ZH_HomeScheduleViewController *vc = [[ZH_HomeScheduleViewController alloc] init];
                vc.idString = self.idString;
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        };
        return cell;
    }
    else if (indexPath.section == 1){
        ZH_CenterInfoTableViewCell *cell = [ZH_CenterInfoTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.dic];
        return cell;
    }
    else if (indexPath.section == 2){
        ZH_BottomInfoTableViewCell *cell = [ZH_BottomInfoTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.viewModel.teacherCourseArray[indexPath.row]];
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 2) {
        ZH_HomeSourceInfoViewController *vc = [[ZH_HomeSourceInfoViewController alloc] init];
        vc.idString = [NSString stringWithFormat:@"%@",self.viewModel.teacherCourseArray[indexPath.row][@"id"]];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_LookTeacherViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_LookTeacherViewModel alloc] init];
    }
    return _viewModel;
}

@end
